import Basic from "./Plugin/basic";
import Geolocation from './Plugin/Geolocation'

class Map {
    constructor () {
        this.basic = new Basic();
        this.Geolocation = new Geolocation(this.basic)
    }

    /**
     * 是否可以使用GPS
     * @param enableHighAccuracy  这个布尔值用来表明应用是否使用其最高精度来表示结果
     * @param maximumAge 它表明可以返回多长时间（即最长年龄，单位毫秒）内的可获取的缓存位置
     * @param timeout 它表明的是设备必须在多长时间（单位毫秒）内返回一个位置
     * @return {Promise<unknown>}
     */
    isLonLat ({enableHighAccuracy = false, maximumAge = 0, timeout = 1000} = {}) {
        let vm = this;
        const options = {...arguments[0]};
        return new Promise(resolve => {
            if (window.navigator.geolocation) {
                console.log('geolocation开始');
                //浏览器支持geolocation
                window.navigator.geolocation.getCurrentPosition(
                    function (position) {
                        console.log('geolocation成功');
                        vm.IpOrGps = true;
                        resolve({...position.coords.longitude})
                    },
                    function (error) {
                        console.log('geolocation失败');
                        vm.IpOrGps = false;
                        resolve(false)
                    }, options);
            } else {
                vm.IpOrGps = false;
                resolve(false);
                console.log('浏览器不支持geolocation')
            }
        })
    }


    async getLocation ({type = 'gcj02'} = {}, opts = {}) {
        let vm = this;
        const result = await vm.Geolocation.getCurrentPosition();
        if (!result) return await vm.Geolocation.getCityInfo();
        else return result
    }


    onError (error) {
        switch (error.code) {
            case 1:
                alert("位置服务被拒绝");
                break;
            case 2:
                alert("暂时获取不到位置信息");
                break;
            case 3:
                alert("获取信息超时");
                break;
            case 4:
                alert("未知错误");
                break;
        }
    }
}

export default new Map()
